--變化人爲
local bianhuarenwei = fk.CreateSkill {
    name = "mcdh_bianhuarenwei",
}

local U = require "packages/utility/utility"
local lib = require "packages/mcdh/lib"

bianhuarenwei:addEffect('active',{
    card_num = 0,
    target_num = 1,
    can_use = function(self, player)
        return not player:isKongcheng() and player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
    end,
    card_filter = Util.FalseFunc,
    target_filter = function(self,player, to_select, selected)
        return #selected == 0 and to_select ~= player and player:canPindian(to_select)
    end,
    on_use = function(self, room, effect)
        local player = effect.from
        local target = effect.tos[1]
        local pindian = player:pindian({target}, self.name)
        if pindian.results[target].winner == player then
            local to, targets, cards
            local total = 2
            local n = total
            repeat
                targets = table.filter(room.alive_players, function(p)return not p:isNude() end)

                if #targets == 0 then break end
                targets = room:askToChoosePlayers(player, {
                    targets = targets,
                    min_num = 1 ,
                    max_num= 1,
                    prompt="#bianhuarenwei-choose:::"..tostring(total)..":"..tostring(n),
                    skill_name = self.name
            })
                if #targets == 0 then break end
                to = targets[1]
                cards = room:askToChooseCards(player, {target = to,min =1,max=n,flag = "he",skill_name = self.name})
                room:throwCard(cards, self.name, to, player)
                to:drawCards(#cards,self.name)
                n = n - #cards
                if n <= 0 then break end
            until total == 0 or player.dead
        else
            if not player:isNude() then
                room:askToDiscard(player,{min_num = 1 ,max_num = 1,skill_name = self.name,cancelable = false,prompt="#bianhuarenwei-discard",include_equip=true} )
            end
        end
    end
})

Fk:loadTranslationTable {
    ["mcdh_bianhuarenwei"] = "變化人爲",
    [":mcdh_bianhuarenwei"] = "出牌阶段限一次，你可以拼点，若你：赢，你可以弃置任意名角色的共计至多两张牌，每弃置一张牌，被弃置牌的角色摸一张牌；没赢，你弃置一张牌。",
    ["#bianhuarenwei-choose"] = "變化人爲：你可以依次选择角色，弃置其的牌（共计至多%arg张，还剩%arg2张）",
    ["#bianhuarenwei-discard"] = "變化人爲：弃置一张牌",
}

return bianhuarenwei